package com.xbdun.web.validatebmp.upload.service.manage;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.xbdun.dao.validatebmp.entity.EcUploadFileDetail;
import com.xbdun.dao.validatebmp.entity.EcUploadFileInfo;
import com.xbdun.dao.validatebmp.mapper.UploadFileMapper;
import com.xbdun.facade.validatebmp.exceptions.ValidateBizException;
import com.xbdun.facade.validatebmp.menus.UploadFileFormat;
/**
 * 文件上传管理业务类
 */
@Service
public class UploadFileManager {
	private static final String IMAGE_CACH_NAME = "IMAGE_CACH_NAME";
	@Autowired
	private UploadFileMapper uploadFileMapper;
	/**
	 * 获取上传文件详情信息
	 * @param uploadFileId 上传文件Id
	 * @return EcUploadFileDetail
	 * @throws Exception
	 */
	@Cacheable(value=IMAGE_CACH_NAME,key="#uploadFileId+'_'+#fileFormat")
	public EcUploadFileDetail queryFileDetail(long uploadFileId,int fileFormat) throws Exception{
		List<EcUploadFileDetail> list = uploadFileMapper.queryFileDetail(uploadFileId,fileFormat);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 获取上传文件详情信息
	 * @param uploadFileId 上传文件Id
	 * @return List<EcUploadFileDetail>
	 */
	@Cacheable(value=IMAGE_CACH_NAME,key="#uploadFileId")
	public List<EcUploadFileDetail> queryFileDetails(long uploadFileId) throws Exception{
		return uploadFileMapper.queryFileDetail(uploadFileId,-1);
	}
	
	/**
	 * 保存文件信息
	 * @param uploadFileInfo
	 * @return
	 */
	public int saveFileInfo(EcUploadFileInfo uploadFileInfo) throws Exception{
		return uploadFileMapper.saveFileInfo(uploadFileInfo);
	}
	
	/**
	 * 保存文件详情
	 * @param fileDetails
	 */
	public void saveFileDetail(List<EcUploadFileDetail> fileDetails) throws Exception{
		uploadFileMapper.saveFileDetail(fileDetails);
	}
	/**
	 * 删除文件信息
	 * @param uploadFileId
	 * @return
	 */
	public int delFileInfo(long uploadFileId) throws Exception{
		return uploadFileMapper.delFileInfo(uploadFileId);
	}
	
	//删除缓存
	@CacheEvict(value=IMAGE_CACH_NAME,key="#uploadFileId")
	public void deFileInfoCache(long uploadFileId){}
	
	/**
	 * 删除文件信息
	 * @param ids 
	 * @return
	 */
	public int delFileInfoByIds(String ids) throws Exception{
		return uploadFileMapper.delFileInfoByIds(ids);
	}
	/**
	 * 获取上传文件中大小（字节）
	 * @return
	 */
	public long getTotalSize() throws Exception{
		return uploadFileMapper.getTotalSize();
	}
	/**
	 * 获取上传文件总数量 包括已经删除的文件
	 * @return
	 */
	public long getTotalQuantity() throws Exception{
		return uploadFileMapper.getTotalQuantity();
	}
	/**
	 * 获取已经删除的文件总数量
	 * @return
	 */
	public long getTotalDeletedQuantity() throws Exception{
		return uploadFileMapper.getTotalDeletedQuantity();
	}
	
	/**
	 * 获取上传文件URL地址
	 * @param id 上传文件Id
	 * @param uploadFileFormat 规格{@link UploadFileFormat}
	 * @return URL地址
	 * @throws UploadFileError
	 */
	@Cacheable(value=IMAGE_CACH_NAME,key="#id+'_'+#uploadFileFormat.key")
	public String getUrl(long id,UploadFileFormat uploadFileFormat)throws ValidateBizException{
		
		return "";
	}
	
//	@CacheEvict
	
//	@CachePut
}
